package com.ysd.lis.service.bac.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.*;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.entity.lab.LabReqMain;
import com.ysd.lis.mapper.bac.*;
import com.ysd.lis.mapper.lab.LabMaininfoMapper;
import com.ysd.lis.mapper.lab.LabReqMainMapper;
import com.ysd.lis.request.lab.ChangeWorkFlowParam;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.bac.*;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 微生物检验应用流程方案 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-04-11
 */
@Service
public class BacTestWorkFlowServiceImpl extends ServiceImpl<BacTestWorkFlowMapper, BacTestWorkFlow> implements BacTestWorkFlowService {
    @Autowired
    BacTestWorkFlowMapper bacTestWorkFlowMapper;
    @Autowired
    BacTestWorkFlowNodeMapper bacTestWorkFlowNodeMapper;
    @Autowired
    BacTestWorkFlowNodeService bacTestWorkFlowNodeService;
    @Autowired
    BacTestWorkFlowService bacTestWorkFlowService;
    @Autowired
    BacTestResultService bacTestResultService;
    @Autowired
    BacTestResultMapper bacTestResultMapper;
    @Autowired
    BacDrugTestResultService bacDrugTestResultService;
    @Autowired
    BacDrugTestResultMapper bacDrugTestResultMapper;
    @Autowired
    BacWorkFlowMapper bacWorkFlowMapper;
    @Autowired
    BacWorkFlowNodeMapper bacWorkFlowNodeMapper;
    @Autowired
    BacStainingResultMapper bacStainingResultMapper;
    @Autowired
    BacStainingResultService bacStainingResultService;
    @Autowired
    BacObserResultService bacObserResultService;

    @Autowired
    BacImageResultService bacImageResultService;
    @Autowired
    BacObserResultMapper bacObserResultMapper;
    @Autowired
    BacTestCultureMediumMapper bacTestCultureMediumMapper;
    @Autowired
    BacTestCultureMediumService bacTestCultureMediumService;
    @Autowired
    LabReqMainMapper labReqMainMapper;
    @Autowired
    LabMaininfoMapper labMaininfoMapper;
    @Autowired
    SysRuleSetService sysRuleSetService;//编码规则服务
    @Autowired
    BacReqPurposeMapper bacReqPurposeMapper;
    @Autowired
    BacCultureCombinationMapper bacCultureCombinationMapper;//培养基组合
    @Autowired
    BacCultureCombinaRelMapper bacCultureCombinaRelMapper;//培养基组合明细
    @Autowired
    BacPublisSecReportService bacPublisSecReportService;//

    @Autowired
    BacStrainMapper bacStrainMapper;

    @Autowired
    BacObserResultNoGermMapper bacObserResultNoGermMapper;
    @Override
    public Result findBacTestWorkFlowList(BacTestWorkFlow bacTestWorkFlow) {
        List<BacTestWorkFlow> list = findBacTestWorkFlowArray(bacTestWorkFlow);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public Result findBacTestWorkFlowListByReqNo(BacTestWorkFlow bacTestWorkFlow) {
        List<BacTestWorkFlow> list = findBacTestWorkFlowArrayByReqNo(bacTestWorkFlow);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacTestWorkFlow> findBacTestWorkFlowArray(BacTestWorkFlow bacTestWorkFlow) {
        /*if (org.apache.commons.lang3.StringUtils.isEmpty(bacTestWorkFlow.getReqNo())) {
            return null;
        }*/
        LambdaQueryWrapper<LabMaininfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabMaininfo::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            wrapper.ge(LabMaininfo::getSampleda, startTime);
            wrapper.le(LabMaininfo::getSampleda, endTime);
        }
        wrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), LabMaininfo::getSampleNo, bacTestWorkFlow.getSampleNo());
        wrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), LabMaininfo::getRptGroup, bacTestWorkFlow.getRptGroup());
        wrapper.eq(StringUtils.isNotBlank(bacTestWorkFlow.getReqNo()), LabMaininfo::getReqNo, bacTestWorkFlow.getReqNo());
        LabMaininfo maininfo = labMaininfoMapper.selectOne(wrapper);


        LambdaQueryWrapper<BacStrain> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapper.ge(BacStrain::getSampleda, startTime);
            lambdaQueryWrapper.le(BacStrain::getSampleda, endTime);
        }
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), BacStrain::getSampleNo, bacTestWorkFlow.getSampleNo());
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), BacStrain::getRptGroup, bacTestWorkFlow.getRptGroup());
        lambdaQueryWrapper.eq(BacStrain::getDelFlag, 0);
        Integer count = bacStrainMapper.selectCount(lambdaQueryWrapper);


        LambdaQueryWrapper<BacObserResultNoGerm> obserWrapper = new LambdaQueryWrapper<>();
        if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            obserWrapper.ge(BacObserResultNoGerm::getSampleda, startTime);
            obserWrapper.le(BacObserResultNoGerm::getSampleda, endTime);
        }
        obserWrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), BacObserResultNoGerm::getSampleNo, bacTestWorkFlow.getSampleNo());
        obserWrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), BacObserResultNoGerm::getRptGroup, bacTestWorkFlow.getRptGroup());
        obserWrapper.eq(BacObserResultNoGerm::getDelFlag, 0);

        Integer count1 = bacObserResultNoGermMapper.selectCount(obserWrapper);

        MPJLambdaWrapper<BacTestWorkFlow> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacTestWorkFlow::getDelFlag, 0);

        if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            queryWrapper.ge(BacTestWorkFlow::getSampleda, startTime);
            queryWrapper.le(BacTestWorkFlow::getSampleda, endTime);
        }
        //queryWrapper.eq(BeanUtil.isNotEmpty(bacTestWorkFlow.getSampleda()), BacTestCultureMedium::getSampleda, bacTestWorkFlow.getSampleda());
        queryWrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), BacTestWorkFlow::getSampleNo, bacTestWorkFlow.getSampleNo());
        queryWrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), BacTestWorkFlow::getRptGroup, bacTestWorkFlow.getRptGroup());


        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlow.getReqNo()), BacTestWorkFlow::getReqNo, bacTestWorkFlow.getReqNo());
//        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlow.getReqMainId()), BacTestCultureMedium::getReqMainId, bacTestWorkFlow.getReqMainId());

//        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlow.getWorkFlowId()), BacTestWorkFlow::getWorkFlowId, bacTestWorkFlow.getWorkFlowId());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlow.getWorkFlowNo()), BacTestWorkFlow::getWorkFlowNo, bacTestWorkFlow.getWorkFlowNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlow.getWorkFlowNa()), BacTestWorkFlow::getWorkFlowNa, bacTestWorkFlow.getWorkFlowNa());

        List<BacTestWorkFlow> list = bacTestWorkFlowMapper.selectList(queryWrapper);

        if (!list.isEmpty()) {
            /*查询出子集*/
//            List<String> ids = list.stream().map(BacTestWorkFlow::getId).collect(Collectors.toList());
            List<String> ids = list.stream().map(BacTestWorkFlow::getWorkFlowNo).collect(Collectors.toList());
            MPJLambdaWrapper<BacTestWorkFlowNode> queryWrapperNode = new MPJLambdaWrapper<>();
            queryWrapperNode.selectAll(BacTestWorkFlowNode.class);
//            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getId, BacTestWorkFlowNode::getWorkFlowNodeId);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getNodeCode, BacTestWorkFlowNode::getWorkFlowNodeDictNo);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getNodeName, BacTestWorkFlowNode::getWorkFlowNodeDictNa);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getNodeUrl, BacTestWorkFlowNode::getNodeUrl);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getSeq, BacTestWorkFlowNode::getSeq);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getFlowNo, BacTestWorkFlowNode::getWorkFlowId);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getId, BacTestWorkFlowNode::getNodeDictId);

            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getNodeTypeNo, BacTestWorkFlowNode::getNodeTypeNo);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getNodeTypeNa, BacTestWorkFlowNode::getNodeTypeNa);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getCode, BacTestWorkFlowNode::getNodeDictCode);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getName, BacTestWorkFlowNode::getNodeDictName);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getDisplayFlag, BacTestWorkFlowNode::getDisplayFlag);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getDisplayColor, BacTestWorkFlowNode::getDisplayColor);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getCodeRuleNo, BacTestWorkFlowNode::getCodeRuleNo);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getIsPrintLabel, BacTestWorkFlowNode::getIsPrintLabel);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getLabelTemplateNo, BacTestWorkFlowNode::getLabelTemplateNo);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getExplain, BacTestWorkFlowNode::getExplain);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getPageName, BacTestWorkFlowNode::getPageName);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getOperation, BacTestWorkFlowNode::getOperation);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getPageCode, BacTestWorkFlowNode::getPageCode);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getSeq, BacTestWorkFlowNode::getNodeDictSeq);

            queryWrapperNode.leftJoin(BacWorkFlowNode.class, "rt1", p -> p.eq(BacWorkFlowNode::getFlowNo, BacTestWorkFlowNode::getTestFlowNo).eq(BacWorkFlowNode::getNodeCode, BacTestWorkFlowNode::getNodeNo).eq(BacWorkFlowNode::getDelFlag, 0));
            queryWrapperNode.leftJoin(BacWorkFlowNodeDict.class, "dic", BacWorkFlowNodeDict::getCode, BacWorkFlowNode::getNodeDictNo);
            queryWrapperNode.eq(BacTestWorkFlowNode::getDelFlag, 0);
            if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
                Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate = sdf.format(timestamp);
                Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
                Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
                queryWrapperNode.ge(BacTestWorkFlowNode::getSampleda, startTime);
                queryWrapperNode.le(BacTestWorkFlowNode::getSampleda, endTime);
            }
            //queryWrapper.eq(BeanUtil.isNotEmpty(bacTestWorkFlow.getSampleda()), BacTestCultureMedium::getSampleda, bacTestWorkFlow.getSampleda());
            queryWrapperNode.in(BacTestWorkFlowNode::getTestFlowNo, ids);
            queryWrapperNode.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), BacTestWorkFlowNode::getSampleNo, bacTestWorkFlow.getSampleNo());
            queryWrapperNode.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), BacTestWorkFlowNode::getRptGroup, bacTestWorkFlow.getRptGroup());
            queryWrapperNode.eq(StringUtils.isNotBlank(bacTestWorkFlow.getReqNo()), BacTestWorkFlow::getReqNo, bacTestWorkFlow.getReqNo());

            queryWrapperNode.orderByAsc(BacTestWorkFlowNode::getSeq).orderByAsc(BacWorkFlowNodeDict::getSeq);
            List<BacTestWorkFlowNode> nodeList = bacTestWorkFlowNodeMapper.selectList(queryWrapperNode);
            Timestamp sampleda = bacTestWorkFlow.getSampleda();
            String rptGroup = bacTestWorkFlow.getRptGroup();
            String sampleNo = bacTestWorkFlow.getSampleNo();
            if (ToolsUtils.isNotEmpty(sampleda) && ToolsUtils.isNotEmpty(rptGroup) && ToolsUtils.isNotEmpty(sampleNo)) {
                //查询是否补费
                for (BacTestWorkFlowNode flowNode : nodeList) {
                    if (flowNode.getPageCode().equals("RptResultBac")) {
                        getIsAddFee(flowNode);
                    }
                }
            }
            for (BacTestWorkFlow m : list) {
                //&& m.getReqNo().equals(p.getReqNo())
                //StringUtils.isNotBlank(p.getReqNo()) &&
                List<BacTestWorkFlowNode> _nodeList = nodeList.stream().filter(p -> m.getWorkFlowNo().equals(p.getTestFlowNo())).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(m.getReqNo())) {
                    _nodeList = _nodeList.stream().filter(p -> StringUtils.isNotBlank(p.getReqNo()) && m.getReqNo().equals(p.getReqNo())).collect(Collectors.toList());
                }
                if (!_nodeList.isEmpty()) {
                    int lateNum = 0;
                    if (_nodeList.size() == 1) {
                        _nodeList.get(0).setIsActive(true);
                    } else {
                        if (ToolsUtils.isNotEmpty(maininfo)) {
                            //判断是否审核
                            if (new BigDecimal(2).equals(maininfo.getIsChk())) {
                                //判断_nodeList 里是否包含nodeDictCode的值是JINGJIAN的数据,如果找到返回index
                                for (int i = 0; i < _nodeList.size(); i++) {
                                    if ("JINGJIAN".equals(_nodeList.get(i).getNodeDictCode())) {
                                        //如果找到,则把index赋值给lateNum
                                        lateNum = i;
                                        break;
                                    }
                                }
                                _nodeList.get(lateNum).setIsActive(true);
                            } else {
                                for (int i = 0; i < _nodeList.size(); i++) {
                                    if (count == 0 && count1 == 0) {
                                        if ("PEIYANG".equals(_nodeList.get(i).getNodeDictCode())) {
                                            //如果找到,则把index赋值给lateNum
                                            lateNum = i;
                                            break;
                                        }
                                    } else {
                                        if ("JINGJIAN".equals(_nodeList.get(i).getNodeDictCode())) {
                                            //如果找到,则把index赋值给lateNum
                                            lateNum = i;
                                            break;
                                        }
                                    }
                                }
                                _nodeList.get(lateNum).setIsActive(true);
                            }

                        }
                    }
                    m.setBacTestWorkFlowNodes(_nodeList);
                }
            }
        }
        return list;
    }

    @Autowired
    BacAddFeeMapper bacAddFeeMapper;

    private void getIsAddFee(BacTestWorkFlowNode flowNode) {
        Timestamp sampleda = flowNode.getSampleda();
        String rptGroup = flowNode.getRptGroup();
        String sampleNo = flowNode.getSampleNo();
        LambdaQueryWrapper<BacDrugTestResult> queryWrapperMR = new LambdaQueryWrapper<>();
        queryWrapperMR.eq(BacDrugTestResult::getDelFlag, 0)
                .eq(BacDrugTestResult::getSampleNo, sampleNo)
                .eq(BacDrugTestResult::getRptGroup, rptGroup);
        if (!BeanUtil.isEmpty(sampleda)) {
            Timestamp startTime = Timestamp.valueOf(sampleda.toString().substring(0, 10) + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(sampleda.toString().substring(0, 10) + " 23:59:59");
            queryWrapperMR.ge(BacDrugTestResult::getSampleda, startTime);
            queryWrapperMR.le(BacDrugTestResult::getSampleda, endTime);
        }
        Integer rPlanCnt = bacDrugTestResultMapper.selectCount(queryWrapperMR);
        if (rPlanCnt > 0) {
            LambdaQueryWrapper<BacAddFee> queryWrapperFee = new LambdaQueryWrapper<>();
            queryWrapperFee.eq(BacAddFee::getDelFlag, 0)
                    .eq(BacAddFee::getSampleNo, sampleNo)
                    .eq(BacAddFee::getRptGroup, rptGroup);
            if (!BeanUtil.isEmpty(sampleda)) {
                Timestamp startTime = Timestamp.valueOf(sampleda.toString().substring(0, 10) + " 00:00:00");
                Timestamp endTime = Timestamp.valueOf(sampleda.toString().substring(0, 10) + " 23:59:59");
                queryWrapperFee.ge(BacAddFee::getSampleda, startTime);
                queryWrapperFee.le(BacAddFee::getSampleda, endTime);
            }
            Integer feeCnt = bacAddFeeMapper.selectCount(queryWrapperFee);
            if (feeCnt > 0) {
                flowNode.setAddFee(0);
                flowNode.setAddFeeDesc("已补费");
            } else {
                flowNode.setAddFee(1);
                flowNode.setAddFeeDesc("未补费");
            }
        } else {
            flowNode.setAddFeeDesc("-");
        }
    }

    @Override
    public List<BacTestWorkFlow> findBacTestWorkFlowArrayByReqNo(BacTestWorkFlow bacTestWorkFlow) {
        if (org.apache.commons.lang3.StringUtils.isEmpty(bacTestWorkFlow.getReqNo())) {
            return null;
        }
        MPJLambdaWrapper<BacTestWorkFlow> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacTestWorkFlow::getDelFlag, 0);


        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlow.getReqNo()), BacTestWorkFlow::getReqNo, bacTestWorkFlow.getReqNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlow.getWorkFlowNo()), BacTestWorkFlow::getWorkFlowNo, bacTestWorkFlow.getWorkFlowNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlow.getWorkFlowNa()), BacTestWorkFlow::getWorkFlowNa, bacTestWorkFlow.getWorkFlowNa());

        List<BacTestWorkFlow> list = bacTestWorkFlowMapper.selectList(queryWrapper);

        if (!list.isEmpty()) {
            /*查询出子集*/
//            List<String> ids = list.stream().map(BacTestWorkFlow::getId).collect(Collectors.toList());
            List<String> ids = list.stream().map(BacTestWorkFlow::getWorkFlowNo).collect(Collectors.toList());
            MPJLambdaWrapper<BacTestWorkFlowNode> queryWrapperNode = new MPJLambdaWrapper<>();
            queryWrapperNode.selectAll(BacTestWorkFlowNode.class);
//            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getId, BacTestWorkFlowNode::getWorkFlowNodeId);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getNodeCode, BacTestWorkFlowNode::getWorkFlowNodeDictNo);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getNodeName, BacTestWorkFlowNode::getWorkFlowNodeDictNa);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getNodeUrl, BacTestWorkFlowNode::getNodeUrl);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getSeq, BacTestWorkFlowNode::getSeq);
            queryWrapperNode.selectAs("rt1", BacWorkFlowNode::getFlowNo, BacTestWorkFlowNode::getWorkFlowId);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getId, BacTestWorkFlowNode::getNodeDictId);

            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getNodeTypeNo, BacTestWorkFlowNode::getNodeTypeNo);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getNodeTypeNa, BacTestWorkFlowNode::getNodeTypeNa);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getCode, BacTestWorkFlowNode::getNodeDictCode);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getName, BacTestWorkFlowNode::getNodeDictName);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getDisplayFlag, BacTestWorkFlowNode::getDisplayFlag);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getDisplayColor, BacTestWorkFlowNode::getDisplayColor);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getCodeRuleNo, BacTestWorkFlowNode::getCodeRuleNo);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getIsPrintLabel, BacTestWorkFlowNode::getIsPrintLabel);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getLabelTemplateNo, BacTestWorkFlowNode::getLabelTemplateNo);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getExplain, BacTestWorkFlowNode::getExplain);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getPageName, BacTestWorkFlowNode::getPageName);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getOperation, BacTestWorkFlowNode::getOperation);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getPageCode, BacTestWorkFlowNode::getPageCode);
            queryWrapperNode.selectAs("dic", BacWorkFlowNodeDict::getSeq, BacTestWorkFlowNode::getNodeDictSeq);

            queryWrapperNode.leftJoin(BacWorkFlowNode.class, "rt1", p -> p.eq(BacWorkFlowNode::getFlowNo, BacTestWorkFlowNode::getTestFlowNo).eq(BacWorkFlowNode::getNodeCode, BacTestWorkFlowNode::getNodeNo).eq(BacWorkFlowNode::getDelFlag, 0));
            queryWrapperNode.leftJoin(BacWorkFlowNodeDict.class, "dic", BacWorkFlowNodeDict::getCode, BacWorkFlowNode::getNodeDictNo);
            queryWrapperNode.eq(BacTestWorkFlowNode::getDelFlag, 0);
            if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
                Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate = sdf.format(timestamp);
                Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
                Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
                queryWrapperNode.ge(BacTestWorkFlowNode::getSampleda, startTime);
                queryWrapperNode.le(BacTestWorkFlowNode::getSampleda, endTime);
            }
            //queryWrapper.eq(BeanUtil.isNotEmpty(bacTestWorkFlow.getSampleda()), BacTestCultureMedium::getSampleda, bacTestWorkFlow.getSampleda());
            queryWrapperNode.in(BacTestWorkFlowNode::getTestFlowNo, ids);
            queryWrapperNode.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), BacTestWorkFlowNode::getSampleNo, bacTestWorkFlow.getSampleNo());
            queryWrapperNode.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), BacTestWorkFlowNode::getRptGroup, bacTestWorkFlow.getRptGroup());
            queryWrapperNode.orderByAsc(BacTestWorkFlowNode::getSeq).orderByAsc(BacWorkFlowNodeDict::getSeq);
            List<BacTestWorkFlowNode> nodeList = bacTestWorkFlowNodeMapper.selectList(queryWrapperNode);

            for (BacTestWorkFlow m : list) {
                //&& m.getReqNo().equals(p.getReqNo())
                //StringUtils.isNotBlank(p.getReqNo()) &&
                List<BacTestWorkFlowNode> _nodeList = nodeList.stream().filter(p -> m.getWorkFlowNo().equals(p.getTestFlowNo())).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(m.getReqNo())) {
                    _nodeList = _nodeList.stream().filter(p -> StringUtils.isNotBlank(p.getReqNo()) && m.getReqNo().equals(p.getReqNo())).collect(Collectors.toList());
                }
                if (!_nodeList.isEmpty()) {
                    _nodeList.get(0).setIsActive(true);
                    m.setBacTestWorkFlowNodes(_nodeList);
                }
            }
        }
        return list;
    }

    @Override
    public Result addBacTestWorkFlow(BacTestWorkFlow bacTestWorkFlow) {
        int i = bacTestWorkFlowMapper.insert(bacTestWorkFlow);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacTestWorkFlow);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacTestWorkFlow(BacTestWorkFlow bacTestWorkFlow) {
        bacTestWorkFlow.setDelFlag(1);
        int i = bacTestWorkFlowMapper.updateById(bacTestWorkFlow);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacTestWorkFlow);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacTestWorkFlow(BacTestWorkFlow bacTestWorkFlow) {
        int i = bacTestWorkFlowMapper.updateById(bacTestWorkFlow);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacTestWorkFlow);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result changeBacTestWorkFlow(BacTestWorkFlow bacTestWorkFlow) {
        //先删除后添加
        LambdaQueryWrapper<BacTestWorkFlow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacTestWorkFlow::getDelFlag, 0);
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getReqNo()), BacTestWorkFlow::getReqNo, bacTestWorkFlow.getReqNo());
        if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapper.ge(BacTestWorkFlow::getSampleda, startTime);
            lambdaQueryWrapper.le(BacTestWorkFlow::getSampleda, endTime);
        }
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), BacTestWorkFlow::getSampleNo, bacTestWorkFlow.getSampleNo());
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), BacTestWorkFlow::getRptGroup, bacTestWorkFlow.getRptGroup());

        boolean b = bacTestWorkFlowService.saveOrUpdate(bacTestWorkFlow, lambdaQueryWrapper);

        //删除节点
        LambdaQueryWrapper<BacTestWorkFlowNode> lambdaQueryWrapperNode = new LambdaQueryWrapper<>();
        lambdaQueryWrapperNode.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getReqNo()), BacTestWorkFlowNode::getReqNo, bacTestWorkFlow.getReqNo());
        lambdaQueryWrapperNode.eq(BacTestWorkFlowNode::getTestFlowNo, bacTestWorkFlow.getWorkFlowNo());
        lambdaQueryWrapperNode.eq(BacTestWorkFlowNode::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapperNode.ge(BacTestWorkFlowNode::getSampleda, startTime);
            lambdaQueryWrapperNode.le(BacTestWorkFlowNode::getSampleda, endTime);
        }
        lambdaQueryWrapperNode.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), BacTestWorkFlowNode::getSampleNo, bacTestWorkFlow.getSampleNo());
        lambdaQueryWrapperNode.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), BacTestWorkFlowNode::getRptGroup, bacTestWorkFlow.getRptGroup());

        bacTestWorkFlowNodeService.remove(lambdaQueryWrapperNode);

        if (CollectionUtils.isNotEmpty(bacTestWorkFlow.getBacTestWorkFlowNodes())) {
            bacTestWorkFlowNodeService.saveBatch(bacTestWorkFlow.getBacTestWorkFlowNodes());
        }

        /*更新细菌结果*/
        LambdaQueryWrapper<BacTestResult> lambdaQueryWrapperTestResult = new LambdaQueryWrapper<>();
        lambdaQueryWrapperTestResult.eq(BacTestResult::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapperTestResult.ge(BacTestResult::getSampleda, startTime);
            lambdaQueryWrapperTestResult.le(BacTestResult::getSampleda, endTime);
        }
        lambdaQueryWrapperTestResult.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), BacTestResult::getSampleNo, bacTestWorkFlow.getSampleNo());
        lambdaQueryWrapperTestResult.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), BacTestResult::getRptGroup, bacTestWorkFlow.getRptGroup());
        List<BacTestResult> list = bacTestResultMapper.selectList(lambdaQueryWrapperTestResult);

        if (CollectionUtils.isNotEmpty(list)) {
            BacTestWorkFlowNode node = bacTestWorkFlow.getBacTestWorkFlowNodes().get(bacTestWorkFlow.getBacTestWorkFlowNodes().size() - 1);
            for (BacTestResult m : list) {
                m.setTestFlowNo(bacTestWorkFlow.getWorkFlowNo());
                m.setTestNodeNo(ToolsUtils.isNotEmpty(node) ? node.getNodeNo() : "JINGJIAN");
            }
            bacTestResultService.updateBatchById(list);
        }

        /*更新抗生素结果*/
        LambdaQueryWrapper<BacDrugTestResult> lambdaQueryWrapperDrugResult = new LambdaQueryWrapper<>();
        lambdaQueryWrapperDrugResult.eq(BacDrugTestResult::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacTestWorkFlow.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapperDrugResult.ge(BacDrugTestResult::getSampleda, startTime);
            lambdaQueryWrapperDrugResult.le(BacDrugTestResult::getSampleda, endTime);
        }
        lambdaQueryWrapperDrugResult.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getSampleNo()), BacDrugTestResult::getSampleNo, bacTestWorkFlow.getSampleNo());
        lambdaQueryWrapperDrugResult.eq(ToolsUtils.isNotEmpty(bacTestWorkFlow.getRptGroup()), BacDrugTestResult::getRptGroup, bacTestWorkFlow.getRptGroup());
        List<BacDrugTestResult> listDrug = bacDrugTestResultMapper.selectList(lambdaQueryWrapperDrugResult);

        if (CollectionUtils.isNotEmpty(listDrug)) {
            BacTestWorkFlowNode node = bacTestWorkFlow.getBacTestWorkFlowNodes().get(bacTestWorkFlow.getBacTestWorkFlowNodes().size() - 1);
            for (BacDrugTestResult m : listDrug) {
                m.setTestFlowNo(bacTestWorkFlow.getWorkFlowNo());
                m.setTestNodeNo(ToolsUtils.isNotEmpty(node) ? node.getNodeNo() : "JINGJIAN");
            }
            bacDrugTestResultService.updateBatchById(listDrug);
        }
        /*更新染色结果*/
        /*更新观察结果，细菌结果*/

        if (b) {
            return Result.succ(1, "切换成功", "");
        } else {
            return Result.succ(400, "切换失败", "");
        }
    }

    /*切换方案V2*/
    @Override
    public Result changeBacTestWorkFlowV2(ChangeWorkFlowParam input) {
        //先删除后添加
        LambdaQueryWrapper<BacTestWorkFlow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacTestWorkFlow::getDelFlag, 0);
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(input.getReqNo()), BacTestWorkFlow::getReqNo, input.getReqNo());
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapper.ge(BacTestWorkFlow::getSampleda, startTime);
            lambdaQueryWrapper.le(BacTestWorkFlow::getSampleda, endTime);
        }
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacTestWorkFlow::getSampleNo, input.getSampleNo());
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacTestWorkFlow::getRptGroup, input.getRptGroup());
        lambdaQueryWrapper.eq(BacTestWorkFlow::getWorkFlowNo, input.getCurUseWorkFlowNo());
        List<BacTestWorkFlow> workflowList = bacTestWorkFlowMapper.selectList(lambdaQueryWrapper);

        BacTestWorkFlow aimWorkFlowEntry = CollectionUtils.isNotEmpty(workflowList) ? workflowList.get(0) : null;

        /*查询出目标方案*/
        LambdaQueryWrapper<BacWorkFlow> lambdaQueryWrapperAim = new LambdaQueryWrapper<>();
        lambdaQueryWrapperAim.eq(BacWorkFlow::getDelFlag, 0);
        lambdaQueryWrapperAim.eq(BacWorkFlow::getWorkFlowCode, input.getSelWorkFlowNo());
        List<BacWorkFlow> bacWorkFlowList = bacWorkFlowMapper.selectList(lambdaQueryWrapperAim);
        BacWorkFlow aimWorkFlow = CollectionUtils.isNotEmpty(bacWorkFlowList) ? bacWorkFlowList.get(0) : null;


        //现在在用节点
        LambdaQueryWrapper<BacTestWorkFlowNode> lambdaQueryWrapperNode = new LambdaQueryWrapper<>();
        lambdaQueryWrapperNode.eq(ToolsUtils.isNotEmpty(input.getReqNo()), BacTestWorkFlowNode::getReqNo, input.getReqNo());
        lambdaQueryWrapperNode.eq(BacTestWorkFlowNode::getTestFlowNo, input.getCurUseWorkFlowNo());
        lambdaQueryWrapperNode.eq(BacTestWorkFlowNode::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapperNode.ge(BacTestWorkFlowNode::getSampleda, startTime);
            lambdaQueryWrapperNode.le(BacTestWorkFlowNode::getSampleda, endTime);
        }
        lambdaQueryWrapperNode.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacTestWorkFlowNode::getSampleNo, input.getSampleNo());
        lambdaQueryWrapperNode.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacTestWorkFlowNode::getRptGroup, input.getRptGroup());

        /*查询出当前所有节点*/
        List<BacTestWorkFlowNode> nodeList = bacTestWorkFlowNodeMapper.selectList(lambdaQueryWrapperNode);


        /*查询出目标方案节点*/
        LambdaQueryWrapper<BacWorkFlowNode> queryNodeWrapper = new LambdaQueryWrapper<>();
        queryNodeWrapper.eq(BacWorkFlowNode::getDelFlag, 0);
        queryNodeWrapper.eq(BacWorkFlowNode::getFlowNo, input.getSelWorkFlowNo());
        List<BacWorkFlowNode> bacWorkFlowNodeList = bacWorkFlowNodeMapper.selectList(queryNodeWrapper);


        /*待添加节点集合*/
        List<BacTestWorkFlowNode> newNodeList = new ArrayList<>();
        for (BacWorkFlowNode bacWorkFlowNode : bacWorkFlowNodeList) {
            BacTestWorkFlowNode node = new BacTestWorkFlowNode();
            BeanUtil.copyProperties(nodeList.get(0), node, "id", "creator", "editor", "createTime", "editTime", "delFlag");
            node.setNodeNo(bacWorkFlowNode.getNodeCode());
            node.setTestFlowNo(input.getSelWorkFlowNo());
            newNodeList.add(node);
        }

        /*更新细菌结果*/
        LambdaQueryWrapper<BacTestResult> lambdaQueryWrapperTestResult = new LambdaQueryWrapper<>();
        lambdaQueryWrapperTestResult.eq(BacTestResult::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapperTestResult.ge(BacTestResult::getSampleda, startTime);
            lambdaQueryWrapperTestResult.le(BacTestResult::getSampleda, endTime);
        }
        lambdaQueryWrapperTestResult.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacTestResult::getSampleNo, input.getSampleNo());
        lambdaQueryWrapperTestResult.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacTestResult::getRptGroup, input.getRptGroup());
        List<BacTestResult> list = bacTestResultMapper.selectList(lambdaQueryWrapperTestResult);

        if (CollectionUtils.isNotEmpty(list)) {
            BacTestWorkFlowNode node = nodeList.get(nodeList.size() - 1);
            for (BacTestResult m : list) {
                m.setTestFlowNo(input.getSelWorkFlowNo());
                m.setTestNodeNo(ToolsUtils.isNotEmpty(node) ? node.getNodeNo() : "JINGJIAN");
            }
            bacTestResultService.updateBatchById(list);
        }

        /*更新抗生素结果*/
        LambdaQueryWrapper<BacDrugTestResult> lambdaQueryWrapperDrugResult = new LambdaQueryWrapper<>();
        lambdaQueryWrapperDrugResult.eq(BacDrugTestResult::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapperDrugResult.ge(BacDrugTestResult::getSampleda, startTime);
            lambdaQueryWrapperDrugResult.le(BacDrugTestResult::getSampleda, endTime);
        }
        lambdaQueryWrapperDrugResult.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacDrugTestResult::getSampleNo, input.getSampleNo());
        lambdaQueryWrapperDrugResult.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacDrugTestResult::getRptGroup, input.getRptGroup());
        List<BacDrugTestResult> listDrug = bacDrugTestResultMapper.selectList(lambdaQueryWrapperDrugResult);

        if (CollectionUtils.isNotEmpty(listDrug)) {
            BacTestWorkFlowNode node = nodeList.get(nodeList.size() - 1);
            for (BacDrugTestResult m : listDrug) {
                m.setTestFlowNo(input.getSelWorkFlowNo());
                m.setTestNodeNo(ToolsUtils.isNotEmpty(node) ? node.getNodeNo() : "JINGJIAN");
            }
            bacDrugTestResultService.updateBatchById(listDrug);
        }

        /*更新染色结果*/
        LambdaQueryWrapper<BacStainingResult> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(BacStainingResult::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapper1.ge(BacStainingResult::getSampleda, startTime);
            lambdaQueryWrapper1.le(BacStainingResult::getSampleda, endTime);
        }
        lambdaQueryWrapper1.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacStainingResult::getSampleNo, input.getSampleNo());
        lambdaQueryWrapper1.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacStainingResult::getRptGroup, input.getRptGroup());
        List<BacStainingResult> listBacStainingResult = bacStainingResultMapper.selectList(lambdaQueryWrapper1);

        if (CollectionUtils.isNotEmpty(listBacStainingResult)) {
            for (BacStainingResult m : listBacStainingResult) {
                m.setTestFlowNo(input.getSelWorkFlowNo());
                m.setTestNodeNo("PEIYANG");
            }
            bacStainingResultService.updateBatchById(listBacStainingResult);
        }

        /*更新观察结果，细菌结果*/
        LambdaQueryWrapper<BacObserResult> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(BacObserResult::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            wrapper1.ge(BacObserResult::getSampleda, startTime);
            wrapper1.le(BacObserResult::getSampleda, endTime);
        }
        wrapper1.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacObserResult::getSampleNo, input.getSampleNo());
        wrapper1.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacObserResult::getRptGroup, input.getRptGroup());
        List<BacObserResult> listBacObserResult = bacObserResultMapper.selectList(wrapper1);

        if (CollectionUtils.isNotEmpty(listBacObserResult)) {
            for (BacObserResult m : listBacObserResult) {
                m.setTestFlowNo(input.getSelWorkFlowNo());
                m.setTestNodeNo("PEIYANG");
            }
            bacObserResultService.updateBatchById(listBacObserResult);
        }
        /*镜检图片结果*/
        LambdaQueryWrapper<BacImageResult> imageResultWrapper = new LambdaQueryWrapper<>();
        imageResultWrapper.eq(BacImageResult::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            imageResultWrapper.ge(BacImageResult::getSampleda, startTime);
            imageResultWrapper.le(BacImageResult::getSampleda, endTime);
        }
        imageResultWrapper.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacImageResult::getSampleNo, input.getSampleNo());
        imageResultWrapper.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacImageResult::getRptGroup, input.getRptGroup());
        List<BacImageResult> imageResults = bacImageResultService.list(imageResultWrapper);

        if (CollectionUtils.isNotEmpty(imageResults)) {
            for (BacImageResult m : imageResults) {
                m.setTestFlowNo(input.getSelWorkFlowNo());
                m.setTestNodeNo("PEIYANG");
            }
            bacImageResultService.updateBatchById(imageResults);
        }
        /*更新下选用培养基*/
        LambdaQueryWrapper<BacTestCultureMedium> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(BacTestCultureMedium::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            wrapper2.ge(BacTestCultureMedium::getSampleda, startTime);
            wrapper2.le(BacTestCultureMedium::getSampleda, endTime);
        }
        wrapper2.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacTestCultureMedium::getSampleNo, input.getSampleNo());
        wrapper2.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacTestCultureMedium::getRptGroup, input.getRptGroup());
        List<BacTestCultureMedium> listBacTestCultureMedium = bacTestCultureMediumMapper.selectList(wrapper2);

        if (CollectionUtils.isNotEmpty(listBacTestCultureMedium)) {
            for (BacTestCultureMedium m : listBacTestCultureMedium) {
                m.setTestFlowNo(input.getSelWorkFlowNo());
            }
            bacTestCultureMediumService.updateBatchById(listBacTestCultureMedium);
        }

        if (false && CollectionUtils.isEmpty(listBacTestCultureMedium) && ToolsUtils.isNotEmpty(input.getReqNo()) && ToolsUtils.isNotEmpty(input.getSelWorkFlowNo()) && "005".equals(input.getSelWorkFlowNo())) {
            LambdaQueryWrapper<LabReqMain> reqMainWrapper = new LambdaQueryWrapper<>();
            reqMainWrapper.eq(LabReqMain::getDelFlag, 0);
            reqMainWrapper.eq(LabReqMain::getReqNo, input.getReqNo());
            List<LabReqMain> labReqMains = labReqMainMapper.selectList(reqMainWrapper);

            LambdaQueryWrapper<LabMaininfo> maininfoWrapper = new LambdaQueryWrapper<>();
            maininfoWrapper.eq(LabMaininfo::getDelFlag, 0);
            maininfoWrapper.eq(LabMaininfo::getReqNo, input.getReqNo());
            if (ToolsUtils.isNotEmpty(input.getSampleda())) {
                Timestamp timestamp1 = new Timestamp(input.getSampleda().getTime());
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate1 = sdf1.format(timestamp1);
                Timestamp startTime1 = Timestamp.valueOf(formattedDate1 + " 00:00:00");
                Timestamp endTime1 = Timestamp.valueOf(formattedDate1 + " 23:59:59");
                maininfoWrapper.ge(LabMaininfo::getSampleda, startTime1);
                maininfoWrapper.le(LabMaininfo::getSampleda, endTime1);
            }
            maininfoWrapper.eq(LabMaininfo::getRptGroup, input.getRptGroup());
            maininfoWrapper.eq(LabMaininfo::getSampleNo, input.getSampleNo());
            List<LabMaininfo> labMaininfos = labMaininfoMapper.selectList(maininfoWrapper);

            if (CollectionUtils.isNotEmpty(labReqMains) && CollectionUtils.isNotEmpty(labMaininfos)) {
                CalcWkflwCultures(labReqMains.get(0), labMaininfos.get(0));
            }
        }
        if (ToolsUtils.isNotEmpty(input.getPurposeNo())) {
            LambdaQueryWrapper<LabReqMain> reqMainWrapper = new LambdaQueryWrapper<>();
            reqMainWrapper.eq(LabReqMain::getDelFlag, 0);
            reqMainWrapper.eq(LabReqMain::getReqNo, input.getReqNo());
            List<LabReqMain> labReqMains = labReqMainMapper.selectList(reqMainWrapper);

            LambdaQueryWrapper<LabMaininfo> maininfoWrapper = new LambdaQueryWrapper<>();
            maininfoWrapper.eq(LabMaininfo::getDelFlag, 0);
            maininfoWrapper.eq(LabMaininfo::getReqNo, input.getReqNo());
            if (ToolsUtils.isNotEmpty(input.getSampleda())) {
                Timestamp timestamp1 = new Timestamp(input.getSampleda().getTime());
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate1 = sdf1.format(timestamp1);
                Timestamp startTime1 = Timestamp.valueOf(formattedDate1 + " 00:00:00");
                Timestamp endTime1 = Timestamp.valueOf(formattedDate1 + " 23:59:59");
                maininfoWrapper.ge(LabMaininfo::getSampleda, startTime1);
                maininfoWrapper.le(LabMaininfo::getSampleda, endTime1);
            }
            maininfoWrapper.eq(LabMaininfo::getRptGroup, input.getRptGroup());
            maininfoWrapper.eq(LabMaininfo::getSampleNo, input.getSampleNo());
            List<LabMaininfo> labMaininfos = labMaininfoMapper.selectList(maininfoWrapper);

            if (CollectionUtils.isNotEmpty(labReqMains) && CollectionUtils.isNotEmpty(labMaininfos)) {
                LabReqMain reqMain = labReqMains.get(0);
                reqMain.setPurposeNo(input.getPurposeNo());
                bacChangeFlowCalcCultureMedium(reqMain, labMaininfos.get(0), input.getSelWorkFlowNo());
            }
        }

        /*删除所有的节点*/
        boolean r;
        LambdaQueryWrapper<BacTestWorkFlowNode> lambdaAllWrapperNode = new LambdaQueryWrapper<>();
        lambdaAllWrapperNode.eq(ToolsUtils.isNotEmpty(input.getReqNo()), BacTestWorkFlowNode::getReqNo, input.getReqNo());
        lambdaAllWrapperNode.eq(BacTestWorkFlowNode::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaAllWrapperNode.ge(BacTestWorkFlowNode::getSampleda, startTime);
            lambdaAllWrapperNode.le(BacTestWorkFlowNode::getSampleda, endTime);
        }
        lambdaAllWrapperNode.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacTestWorkFlowNode::getSampleNo, input.getSampleNo());
        lambdaAllWrapperNode.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacTestWorkFlowNode::getRptGroup, input.getRptGroup());
        r = bacTestWorkFlowNodeService.remove(lambdaAllWrapperNode);
        /*插入新的节点*/
        r = bacTestWorkFlowNodeService.saveBatch(newNodeList);


        /*删除所有的方案,再去新增*/
        LambdaQueryWrapper<BacTestWorkFlow> lambdaWorkFlowAllWrapper = new LambdaQueryWrapper<>();
        lambdaWorkFlowAllWrapper.eq(BacTestWorkFlow::getDelFlag, 0);
        lambdaWorkFlowAllWrapper.eq(ToolsUtils.isNotEmpty(input.getReqNo()), BacTestWorkFlow::getReqNo, input.getReqNo());
        if (ToolsUtils.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaWorkFlowAllWrapper.ge(BacTestWorkFlow::getSampleda, startTime);
            lambdaWorkFlowAllWrapper.le(BacTestWorkFlow::getSampleda, endTime);
        }
        lambdaWorkFlowAllWrapper.eq(ToolsUtils.isNotEmpty(input.getSampleNo()), BacTestWorkFlow::getSampleNo, input.getSampleNo());
        lambdaWorkFlowAllWrapper.eq(ToolsUtils.isNotEmpty(input.getRptGroup()), BacTestWorkFlow::getRptGroup, input.getRptGroup());
        bacTestWorkFlowService.remove(lambdaWorkFlowAllWrapper);

        aimWorkFlowEntry.setId(null);
        aimWorkFlowEntry.setWorkFlowNo(input.getSelWorkFlowNo());
        aimWorkFlowEntry.setWorkFlowNa(aimWorkFlow.getWorkFlowName());
        r = bacTestWorkFlowService.saveOrUpdate(aimWorkFlowEntry);
        /*切换方案,二级报告数据更改原有方案的,状态改为取消*/
        LambdaUpdateWrapper<BacPublisSecReport> secReportWrapper = new LambdaUpdateWrapper<>();
        secReportWrapper.eq(BacPublisSecReport::getSampleda, input.getSampleda());
        secReportWrapper.eq(BacPublisSecReport::getRptGroup, input.getRptGroup());
        secReportWrapper.eq(BacPublisSecReport::getSampleNo, input.getSampleNo());
        secReportWrapper.eq(BacPublisSecReport::getTestFlowNo, input.getCurUseWorkFlowNo());
        secReportWrapper.set(BacPublisSecReport::getIsCancel, 1);
        bacPublisSecReportService.update(secReportWrapper);
        if (r) {
            return Result.succ(1, "切换成功", true);
        } else {
            return Result.succ(400, "切换失败", false);
        }
    }

    /*获取全部方案并标记处当前使用*/
    @Override
    public Result findAllWorkFlowWithCurrentUse(BacTestWorkFlow bacTestWorkFlow) {
        /*查询出所有的方案*/
        LambdaQueryWrapper<BacWorkFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacWorkFlow::getDelFlag, 0);
        List<BacWorkFlow> bacWorkFlowList = bacWorkFlowMapper.selectList(queryWrapper);

        if (ToolsUtils.isEmpty(bacWorkFlowList)) {
            return Result.fail("查不到方案数据");
        }

        List<String> wfNos = bacWorkFlowList.stream().map(BacWorkFlow::getWorkFlowCode).collect(Collectors.toList());

        /*查询所有方案节点*/
        LambdaQueryWrapper<BacWorkFlowNode> queryNodeWrapper = new LambdaQueryWrapper<>();
        queryNodeWrapper.eq(BacWorkFlowNode::getDelFlag, 0);
        queryNodeWrapper.in(BacWorkFlowNode::getFlowNo, wfNos);
        List<BacWorkFlowNode> bacWorkFlowNodeList = bacWorkFlowNodeMapper.selectList(queryNodeWrapper);

        /*当前正在使用的方案节点*/
        List<BacTestWorkFlow> list = findBacTestWorkFlowArray(bacTestWorkFlow);

        List<Map<String, Object>> resultList = new ArrayList<>();
        for (BacWorkFlow m : bacWorkFlowList) {
            Map<String, Object> map = new HashMap<>();
            map.put("workFlowNo", m.getWorkFlowCode());
            map.put("workFlowNa", m.getWorkFlowName());
            List<BacTestWorkFlow> aimEntries = list.stream().filter(item -> item.getWorkFlowNo().equals(m.getWorkFlowCode())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(aimEntries)) {
                map.put("isActive", true);
            } else {
                map.put("isActive", false);
            }

            /*组合节点数组*/
            /*1、先找出该方案下所有的节点*/
            List<BacWorkFlowNode> nodeList = bacWorkFlowNodeList.stream().filter(item -> item.getFlowNo().equals(m.getWorkFlowCode())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(nodeList)) {
                //map.put("bacTestWorkFlowNodes", nodeList);
                List<Map<String, Object>> nodeListMap = new ArrayList<>();
                for (BacWorkFlowNode n : nodeList) {
                    Map<String, Object> nodeMap = new HashMap<>();
                    nodeMap.put("nodeNo", n.getNodeCode());
                    nodeMap.put("nodeDictName", n.getNodeName());
                    nodeListMap.add(nodeMap);
                }
                map.put("bacTestWorkFlowNodes", nodeListMap);
            }
            resultList.add(map);
        }

        return Result.succ(1, "成功", resultList);
    }

    /*微生物:计算应用方案、培养基等*/
    public Boolean CalcWkflwCultures(LabReqMain entry, LabMaininfo labMaininfo) {
        List<BacTestWorkFlow> BacTestWorkFlowList = new ArrayList<>();
        List<BacTestWorkFlowNode> BacTestWorkFlowNodeList = new ArrayList<>();
        List<BacTestCultureMedium> BacTestCultureMediumList = new ArrayList<>();

        /*第一步：查询检验目的对照的方案步骤培养基组合插入到方案 步骤表 所用培养基*/
        if (!org.apache.commons.lang3.StringUtils.isEmpty(entry.getPurposeNo())) {
            /*1、查询出检验目的*/
            MPJLambdaWrapper<BacReqPurpose> bacReqPurposeWrapper = new MPJLambdaWrapper<>();
            bacReqPurposeWrapper.eq(BacReqPurpose::getDelFlag, 0);
            bacReqPurposeWrapper.eq(BacReqPurpose::getPurposeNo, entry.getPurposeNo());
            List<BacReqPurpose> reqPurposeEntries = bacReqPurposeMapper.selectList(bacReqPurposeWrapper);
            BacReqPurpose reqPurposeEntry = reqPurposeEntries.isEmpty() ? null : reqPurposeEntries.get(0);

            /*2、获取检验目的默认的方案、培养基组合 及流程节点、培养基*/
            BacWorkFlow reqWorkFlowEntry = null;
            List<BacWorkFlowNode> reqWorkFlowNodeList = null;
            if (!BeanUtil.isEmpty(reqPurposeEntry) && org.apache.commons.lang3.StringUtils.isNotEmpty(reqPurposeEntry.getFlowNo())) {
                MPJLambdaWrapper<BacWorkFlow> bacWorkFlowWrapper = new MPJLambdaWrapper<>();
                bacWorkFlowWrapper.eq(BacWorkFlow::getDelFlag, 0);
                bacWorkFlowWrapper.and(p -> p.eq(BacWorkFlow::getWorkFlowCode, reqPurposeEntry.getFlowNo()).eq(BacWorkFlow::getDelFlag, 0));
                reqWorkFlowEntry = bacWorkFlowMapper.selectOne(bacWorkFlowWrapper);

                /*获取流程方案节点*/
                MPJLambdaWrapper<BacWorkFlowNode> bacWorkFlowNodeWrapper = new MPJLambdaWrapper<>();
                bacWorkFlowNodeWrapper.eq(BacWorkFlowNode::getDelFlag, 0);
                bacWorkFlowNodeWrapper.eq(BacWorkFlowNode::getFlowNo, reqWorkFlowEntry.getWorkFlowCode());
                bacWorkFlowNodeWrapper.orderByAsc(BacWorkFlowNode::getSeq);
                reqWorkFlowNodeList = bacWorkFlowNodeMapper.selectList(bacWorkFlowNodeWrapper);
            }

            /*3、获取培养基、培养基组合*/
            BacCultureCombination reqCultureCombinationEntry = null;
            List<BacCultureCombinaRel> bacCultureCombinaRelList = null;
            if (!BeanUtil.isEmpty(reqPurposeEntry) && org.apache.commons.lang3.StringUtils.isNotEmpty(reqPurposeEntry.getCulComninaNo())) {
                MPJLambdaWrapper<BacCultureCombination> bacCultureCombinationWrapper = new MPJLambdaWrapper<>();
                bacCultureCombinationWrapper.eq(BacCultureCombination::getDelFlag, 0);
                bacCultureCombinationWrapper.eq(BacCultureCombination::getCode, reqPurposeEntry.getCulComninaNo());
                List<BacCultureCombination> reqCultureCombinationEntries = bacCultureCombinationMapper.selectList(bacCultureCombinationWrapper);
                reqCultureCombinationEntry = reqCultureCombinationEntries.isEmpty() ? null : reqCultureCombinationEntries.get(0);

                //bacCultureCombinaRelMapper
                MPJLambdaWrapper<BacCultureCombinaRel> BacCultureCombinaRelWrapper = new MPJLambdaWrapper<>();
                BacCultureCombinaRelWrapper.eq(BacCultureCombinaRel::getDelFlag, 0);
                BacCultureCombinaRelWrapper.and(p -> p.eq(BacCultureCombinaRel::getCulCombinaNo, reqPurposeEntry.getCulComninaNo()).eq(BacCultureCombinaRel::getDelFlag, 0));
                BacCultureCombinaRelWrapper.selectAll(BacCultureCombinaRel.class);
                BacCultureCombinaRelWrapper.selectAs("tab1", BacCultureMedium::getCode, BacCultureCombinaRel::getCultrueNo);
                BacCultureCombinaRelWrapper.selectAs("tab1", BacCultureMedium::getName, BacCultureCombinaRel::getCultrueName);
                BacCultureCombinaRelWrapper.selectAs("tab2", BacCultureCombination::getType, BacCultureCombinaRel::getKind);
                BacCultureCombinaRelWrapper.selectAs("tab2", BacCultureCombination::getCode, BacCultureCombinaRel::getCombinationNo);
                BacCultureCombinaRelWrapper.selectAs("tab2", BacCultureCombination::getName, BacCultureCombinaRel::getCombinationNa);
                BacCultureCombinaRelWrapper.leftJoin(BacCultureMedium.class, "tab1", p -> p.eq(BacCultureMedium::getCode, BacCultureCombinaRel::getCulMediumNo).eq(BacCultureMedium::getDelFlag, 0));
                BacCultureCombinaRelWrapper.leftJoin(BacCultureCombination.class, "tab2", p -> p.eq(BacCultureCombination::getCode, BacCultureCombinaRel::getCulCombinaNo).eq(BacCultureCombination::getDelFlag, 0));

                bacCultureCombinaRelList = bacCultureCombinaRelMapper.selectList(BacCultureCombinaRelWrapper);
            }

            /*4、插入检验 选用方案  节点  培养基信息*/


            String bacTestWkflwId = IdUtil.getSnowflake().nextId() + "";

            //流程方案
            if (reqWorkFlowEntry != null && BeanUtil.isNotEmpty(reqWorkFlowEntry)) {
                BacTestWorkFlow m = new BacTestWorkFlow();
                m.setId(bacTestWkflwId);
                //m.setReqMainId(entry.getId());
                //m.setRptGroup();
                m.setReqNo(entry.getReqNo());
                m.setWorkFlowNo(reqWorkFlowEntry.getWorkFlowCode());
                m.setWorkFlowNa(reqWorkFlowEntry.getWorkFlowName());
                m.setSeq(1);
                m.setCulCombinaNos(reqCultureCombinationEntry.getCode());
                if (BeanUtil.isNotEmpty(labMaininfo)) {
                    m.setRptGroup(labMaininfo.getRptGroup());
                    m.setSampleda(labMaininfo.getSampleda());
                    m.setSampleNo(labMaininfo.getSampleNo());
                }
                BacTestWorkFlowList.add(m);
            }

            //流程方案节点
            if (!BeanUtil.isEmpty(reqWorkFlowNodeList) && !reqWorkFlowNodeList.isEmpty()) {
                for (BacWorkFlowNode m : reqWorkFlowNodeList) {
                    BacTestWorkFlowNode op = new BacTestWorkFlowNode();
                    op.setTestFlowNo(reqWorkFlowEntry.getWorkFlowCode());
                    op.setNodeNo(m.getNodeCode());
                    op.setReqNo(entry.getReqNo());
                    op.setSeq(m.getSeq());
                    if (ToolsUtils.isNotEmpty(labMaininfo)) {
                        op.setRptGroup(labMaininfo.getRptGroup());
                        op.setSampleda(labMaininfo.getSampleda());
                        op.setSampleNo(labMaininfo.getSampleNo());
                    }
                    BacTestWorkFlowNodeList.add(op);
                }
            }

            //选用的培养基
            if (!BeanUtil.isEmpty(bacCultureCombinaRelList) && !bacCultureCombinaRelList.isEmpty()) {
                for (BacCultureCombinaRel m : bacCultureCombinaRelList) {
                    BacTestCultureMedium op = new BacTestCultureMedium();
//                    op.setReqMainId(entry.getId());
                    op.setReqNo(entry.getReqNo());
                    op.setTestFlowNo(reqWorkFlowEntry.getWorkFlowCode());
                    //op.setWorkFlowNodeId(m.getId());
                    String a = sysRuleSetService.createRuleCodeList("CultuBarCode", 1).get(0);
                    op.setAutoNo(a);
                    op.setCultureNo(m.getCultrueNo());
                    op.setCultureNa(m.getCultrueName());
                    op.setCultureCombinNo(m.getCombinationNo());
                    op.setCultureCombinNa(m.getCombinationNa());
                    op.setIsPrintLabel(0);
                    op.setPrintLabelCount(0);
                    //op.setWorkFlowNodeId(m.get);
                    try {
                        op.setSeq(Integer.parseInt(m.getOrderNum()));
                    } catch (Exception e) {
                        //TODO
                    }
                    if (ToolsUtils.isNotEmpty(labMaininfo)) {
                        op.setRptGroup(labMaininfo.getRptGroup());
                        op.setSampleda(labMaininfo.getSampleda());
                        op.setSampleNo(labMaininfo.getSampleNo());
                    }
                    BacTestCultureMediumList.add(op);
                }
            }

            saveWkflwNodesCultuMediums(BacTestWorkFlowList, BacTestWorkFlowNodeList, BacTestCultureMediumList);
        } else {
            bacTestUseDefaultWorkFlowWithPurpose(entry, labMaininfo);
        }
        /*第二步：记录签收状态*/

        return true;
    }

    /*切换方案，计算选用的培养基*/
    public Boolean bacChangeFlowCalcCultureMedium(LabReqMain entry, LabMaininfo labMaininfo, String workFlowNo) {
        List<BacTestWorkFlow> BacTestWorkFlowList = new ArrayList<>();
        List<BacTestWorkFlowNode> BacTestWorkFlowNodeList = new ArrayList<>();
        List<BacTestCultureMedium> BacTestCultureMediumList = new ArrayList<>();

        /*1、查询出检验目的*/
        MPJLambdaWrapper<BacReqPurpose> bacReqPurposeWrapper = new MPJLambdaWrapper<>();
        bacReqPurposeWrapper.eq(BacReqPurpose::getDelFlag, 0);
        bacReqPurposeWrapper.eq(BacReqPurpose::getPurposeNo, entry.getPurposeNo());
        List<BacReqPurpose> reqPurposeEntries = bacReqPurposeMapper.selectList(bacReqPurposeWrapper);
        BacReqPurpose reqPurposeEntry = reqPurposeEntries.isEmpty() ? null : reqPurposeEntries.get(0);

        /*先删除已有的培养基组合、选用的培养基*/
        LambdaQueryWrapper<BacTestCultureMedium> culMediumQueryDel = new LambdaQueryWrapper<>();
        culMediumQueryDel.eq(BacTestCultureMedium::getDelFlag, 0);
        culMediumQueryDel.eq(BacTestCultureMedium::getRptGroup, labMaininfo.getRptGroup());
        culMediumQueryDel.eq(BacTestCultureMedium::getSampleNo, labMaininfo.getSampleNo());
        if (ToolsUtils.isNotEmpty(labMaininfo.getSampleda())) {
            Timestamp timestamp1 = new Timestamp(labMaininfo.getSampleda().getTime());
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate1 = sdf1.format(timestamp1);
            Timestamp startTime1 = Timestamp.valueOf(formattedDate1 + " 00:00:00");
            Timestamp endTime1 = Timestamp.valueOf(formattedDate1 + " 23:59:59");
            culMediumQueryDel.ge(BacTestCultureMedium::getSampleda, startTime1);
            culMediumQueryDel.le(BacTestCultureMedium::getSampleda, endTime1);
        }
        bacTestCultureMediumMapper.delete(culMediumQueryDel);/*删除现有的选中的培养基*/

        /*3、获取培养基、培养基组合*/
        BacCultureCombination reqCultureCombinationEntry = null;
        List<BacCultureCombinaRel> bacCultureCombinaRelList = null;
        if (!BeanUtil.isEmpty(reqPurposeEntry) && org.apache.commons.lang3.StringUtils.isNotEmpty(reqPurposeEntry.getCulComninaNo())) {
            MPJLambdaWrapper<BacCultureCombination> bacCultureCombinationWrapper = new MPJLambdaWrapper<>();
            bacCultureCombinationWrapper.eq(BacCultureCombination::getDelFlag, 0);
            bacCultureCombinationWrapper.eq(BacCultureCombination::getCode, reqPurposeEntry.getCulComninaNo());
            List<BacCultureCombination> reqCultureCombinationEntries = bacCultureCombinationMapper.selectList(bacCultureCombinationWrapper);
            reqCultureCombinationEntry = reqCultureCombinationEntries.isEmpty() ? null : reqCultureCombinationEntries.get(0);

            //bacCultureCombinaRelMapper
            MPJLambdaWrapper<BacCultureCombinaRel> BacCultureCombinaRelWrapper = new MPJLambdaWrapper<>();
            BacCultureCombinaRelWrapper.eq(BacCultureCombinaRel::getDelFlag, 0);
            BacCultureCombinaRelWrapper.and(p -> p.eq(BacCultureCombinaRel::getCulCombinaNo, reqPurposeEntry.getCulComninaNo()).eq(BacCultureCombinaRel::getDelFlag, 0));
            BacCultureCombinaRelWrapper.selectAll(BacCultureCombinaRel.class);
            BacCultureCombinaRelWrapper.selectAs("tab1", BacCultureMedium::getCode, BacCultureCombinaRel::getCultrueNo);
            BacCultureCombinaRelWrapper.selectAs("tab1", BacCultureMedium::getName, BacCultureCombinaRel::getCultrueName);
            BacCultureCombinaRelWrapper.selectAs("tab2", BacCultureCombination::getType, BacCultureCombinaRel::getKind);
            BacCultureCombinaRelWrapper.selectAs("tab2", BacCultureCombination::getCode, BacCultureCombinaRel::getCombinationNo);
            BacCultureCombinaRelWrapper.selectAs("tab2", BacCultureCombination::getName, BacCultureCombinaRel::getCombinationNa);
            BacCultureCombinaRelWrapper.leftJoin(BacCultureMedium.class, "tab1", p -> p.eq(BacCultureMedium::getCode, BacCultureCombinaRel::getCulMediumNo).eq(BacCultureMedium::getDelFlag, 0));
            BacCultureCombinaRelWrapper.leftJoin(BacCultureCombination.class, "tab2", p -> p.eq(BacCultureCombination::getCode, BacCultureCombinaRel::getCulCombinaNo).eq(BacCultureCombination::getDelFlag, 0));

            bacCultureCombinaRelList = bacCultureCombinaRelMapper.selectList(BacCultureCombinaRelWrapper);
        }

        /*4、插入检验 选用方案  节点  培养基信息*/
        String bacTestWkflwId = IdUtil.getSnowflake().nextId() + "";

        //选用的培养基
        if (!BeanUtil.isEmpty(bacCultureCombinaRelList) && !bacCultureCombinaRelList.isEmpty()) {
            for (BacCultureCombinaRel m : bacCultureCombinaRelList) {
                BacTestCultureMedium op = new BacTestCultureMedium();
                op.setReqNo(entry.getReqNo());
                op.setTestFlowNo(workFlowNo);
                String a = sysRuleSetService.createRuleCodeList("CultuBarCode", 1).get(0);
                op.setAutoNo(a);
                op.setCultureNo(m.getCultrueNo());
                op.setCultureNa(m.getCultrueName());
                op.setCultureCombinNo(m.getCombinationNo());
                op.setCultureCombinNa(m.getCombinationNa());
                op.setIsPrintLabel(0);
                op.setPrintLabelCount(0);
                try {
                    op.setSeq(Integer.parseInt(m.getOrderNum()));
                } catch (Exception e) {
                    //TODO
                }
                if (ToolsUtils.isNotEmpty(labMaininfo)) {
                    op.setRptGroup(labMaininfo.getRptGroup());
                    op.setSampleda(labMaininfo.getSampleda());
                    op.setSampleNo(labMaininfo.getSampleNo());
                }
                BacTestCultureMediumList.add(op);
            }
        }

        saveWkflwNodesCultuMediums(BacTestWorkFlowList, BacTestWorkFlowNodeList, BacTestCultureMediumList);

        if (entry != null && BeanUtil.isNotEmpty(entry)) {
            labReqMainMapper.updateById(entry);
        }

        return true;
    }

    /*微生物:保存流程方案、流程方案节点、培养基*/
    /*public boolean saveWkflwNodesCultuMediums(List<BacTestWorkFlow> BacTestWorkFlowList, List<BacTestWorkFlowNode> BacTestWorkFlowNodeList, List<BacTestCultureMedium> BacTestCultureMediumList) {
        for (BacTestWorkFlow m : BacTestWorkFlowList) {
            bacTestWorkFlowMapper.insert(m);
        }

        for (BacTestWorkFlowNode m : BacTestWorkFlowNodeList) {
            bacTestWorkFlowNodeMapper.insert(m);
        }

        for (BacTestCultureMedium m : BacTestCultureMediumList) {
            bacTestCultureMediumMapper.insert(m);
        }

        return true;
    }*/

    /*微生物:保存流程方案、流程方案节点、培养基*/
    public boolean saveWkflwNodesCultuMediums(List<BacTestWorkFlow> BacTestWorkFlowList, List<BacTestWorkFlowNode> BacTestWorkFlowNodeList, List<BacTestCultureMedium> BacTestCultureMediumList) {
        for (BacTestWorkFlow m : BacTestWorkFlowList) {
            bacTestWorkFlowMapper.insert(m);
        }

        for (BacTestWorkFlowNode m : BacTestWorkFlowNodeList) {
            bacTestWorkFlowNodeMapper.insert(m);
        }

        for (BacTestCultureMedium m : BacTestCultureMediumList) {
            bacTestCultureMediumMapper.insert(m);
        }

        return true;
    }


    /*微生物检验采用默认方案-默认检验目的*/
    public Boolean bacTestUseDefaultWorkFlowWithPurpose(LabReqMain reqMain, LabMaininfo labMaininfo) {
        List<BacTestWorkFlow> BacTestWorkFlowList = new ArrayList<>();
        List<BacTestWorkFlowNode> BacTestWorkFlowNodeList = new ArrayList<>();
        List<BacTestCultureMedium> BacTestCultureMediumList = new ArrayList<>();
        /*1、查询出检验目的*/
        MPJLambdaWrapper<BacReqPurpose> bacReqPurposeWrapper = new MPJLambdaWrapper<>();
        bacReqPurposeWrapper.eq(BacReqPurpose::getDelFlag, 0);
        bacReqPurposeWrapper.eq(BacReqPurpose::getPurposeNo, "defaultmd");
        List<BacReqPurpose> reqPurposeEntries = bacReqPurposeMapper.selectList(bacReqPurposeWrapper);
        BacReqPurpose reqPurposeEntry = reqPurposeEntries.isEmpty() ? null : reqPurposeEntries.get(0);

        /*2、获取检验目的默认的方案、培养基组合 及流程节点、培养基*/
        BacWorkFlow reqWorkFlowEntry = null;
        List<BacWorkFlowNode> reqWorkFlowNodeList = null;
        if (!BeanUtil.isEmpty(reqPurposeEntry) && org.apache.commons.lang3.StringUtils.isNotEmpty(reqPurposeEntry.getFlowNo())) {
            MPJLambdaWrapper<BacWorkFlow> bacWorkFlowWrapper = new MPJLambdaWrapper<>();
            bacWorkFlowWrapper.eq(BacWorkFlow::getDelFlag, 0);
            bacWorkFlowWrapper.and(p -> p.eq(BacWorkFlow::getWorkFlowCode, reqPurposeEntry.getFlowNo()).eq(BacWorkFlow::getDelFlag, 0));
            reqWorkFlowEntry = bacWorkFlowMapper.selectOne(bacWorkFlowWrapper);

            /*获取流程方案节点*/
            MPJLambdaWrapper<BacWorkFlowNode> bacWorkFlowNodeWrapper = new MPJLambdaWrapper<>();
            bacWorkFlowNodeWrapper.eq(BacWorkFlowNode::getDelFlag, 0);
            bacWorkFlowNodeWrapper.eq(BacWorkFlowNode::getFlowNo, reqWorkFlowEntry.getWorkFlowCode());
            bacWorkFlowNodeWrapper.orderByAsc(BacWorkFlowNode::getSeq);
            reqWorkFlowNodeList = bacWorkFlowNodeMapper.selectList(bacWorkFlowNodeWrapper);
        }

        /*3、获取培养基、培养基组合*/
        BacCultureCombination reqCultureCombinationEntry = null;
        List<BacCultureCombinaRel> bacCultureCombinaRelList = null;
        if (!BeanUtil.isEmpty(reqPurposeEntry) && org.apache.commons.lang3.StringUtils.isNotEmpty(reqPurposeEntry.getCulComninaNo())) {
            MPJLambdaWrapper<BacCultureCombination> bacCultureCombinationWrapper = new MPJLambdaWrapper<>();
            bacCultureCombinationWrapper.eq(BacCultureCombination::getDelFlag, 0);
            bacCultureCombinationWrapper.eq(BacCultureCombination::getCode, reqPurposeEntry.getCulComninaNo());
            List<BacCultureCombination> reqCultureCombinationEntries = bacCultureCombinationMapper.selectList(bacCultureCombinationWrapper);
            reqCultureCombinationEntry = reqCultureCombinationEntries.isEmpty() ? null : reqCultureCombinationEntries.get(0);

            MPJLambdaWrapper<BacCultureCombinaRel> BacCultureCombinaRelWrapper = new MPJLambdaWrapper<>();
            BacCultureCombinaRelWrapper.eq(BacCultureCombinaRel::getDelFlag, 0);
            BacCultureCombinaRelWrapper.and(p -> p.eq(BacCultureCombinaRel::getCulCombinaNo, reqPurposeEntry.getCulComninaNo()).eq(BacCultureCombinaRel::getDelFlag, 0));
            BacCultureCombinaRelWrapper.selectAll(BacCultureCombinaRel.class);
            BacCultureCombinaRelWrapper.selectAs("tab1", BacCultureMedium::getCode, BacCultureCombinaRel::getCultrueNo);
            BacCultureCombinaRelWrapper.selectAs("tab1", BacCultureMedium::getName, BacCultureCombinaRel::getCultrueName);
            BacCultureCombinaRelWrapper.selectAs("tab2", BacCultureCombination::getType, BacCultureCombinaRel::getKind);
            BacCultureCombinaRelWrapper.selectAs("tab2", BacCultureCombination::getCode, BacCultureCombinaRel::getCombinationNo);
            BacCultureCombinaRelWrapper.selectAs("tab2", BacCultureCombination::getName, BacCultureCombinaRel::getCombinationNa);
            BacCultureCombinaRelWrapper.leftJoin(BacCultureMedium.class, "tab1", p -> p.eq(BacCultureMedium::getCode, BacCultureCombinaRel::getCulMediumNo).eq(BacCultureMedium::getDelFlag, 0));
            BacCultureCombinaRelWrapper.leftJoin(BacCultureCombination.class, "tab2", p -> p.eq(BacCultureCombination::getCode, BacCultureCombinaRel::getCulCombinaNo).eq(BacCultureCombination::getDelFlag, 0));

            bacCultureCombinaRelList = bacCultureCombinaRelMapper.selectList(BacCultureCombinaRelWrapper);
        }

        /*4、插入检验 选用方案  节点  培养基信息*/

        String bacTestWkflwId = IdUtil.getSnowflake().nextId() + "";

        //流程方案
        if (reqWorkFlowEntry != null && BeanUtil.isNotEmpty(reqWorkFlowEntry)) {
            BacTestWorkFlow m = new BacTestWorkFlow();
            m.setId(bacTestWkflwId);
            m.setReqNo(ToolsUtils.isEmpty(reqMain) || ToolsUtils.isEmpty(reqMain.getReqNo()) ? null : reqMain.getReqNo());
            m.setWorkFlowNo(reqWorkFlowEntry.getWorkFlowCode());
            m.setWorkFlowNa(reqWorkFlowEntry.getWorkFlowName());
            m.setSeq(1);
            m.setCulCombinaNos(reqCultureCombinationEntry.getCode());
            if (BeanUtil.isNotEmpty(labMaininfo)) {
                m.setRptGroup(labMaininfo.getRptGroup());
                m.setSampleda(labMaininfo.getSampleda());
                m.setSampleNo(labMaininfo.getSampleNo());
            }
            BacTestWorkFlowList.add(m);
        }

        //流程方案节点
        if (!BeanUtil.isEmpty(reqWorkFlowNodeList) && !reqWorkFlowNodeList.isEmpty()) {
            for (BacWorkFlowNode m : reqWorkFlowNodeList) {
                BacTestWorkFlowNode op = new BacTestWorkFlowNode();
                op.setTestFlowNo(reqWorkFlowEntry.getWorkFlowCode());
                op.setNodeNo(m.getNodeCode());
                op.setReqNo(ToolsUtils.isEmpty(reqMain) || ToolsUtils.isEmpty(reqMain.getReqNo()) ? null : reqMain.getReqNo());
                op.setSeq(m.getSeq());
                if (ToolsUtils.isNotEmpty(labMaininfo)) {
                    op.setRptGroup(labMaininfo.getRptGroup());
                    op.setSampleda(labMaininfo.getSampleda());
                    op.setSampleNo(labMaininfo.getSampleNo());
                }
                BacTestWorkFlowNodeList.add(op);
            }
        }

        //选用的培养基
        if (!BeanUtil.isEmpty(bacCultureCombinaRelList) && !bacCultureCombinaRelList.isEmpty()) {
            for (BacCultureCombinaRel m : bacCultureCombinaRelList) {
                BacTestCultureMedium op = new BacTestCultureMedium();
                op.setReqNo(ToolsUtils.isEmpty(reqMain) || ToolsUtils.isEmpty(reqMain.getReqNo()) ? null : reqMain.getReqNo());
                op.setTestFlowNo(reqWorkFlowEntry.getWorkFlowCode());
                String a = sysRuleSetService.createRuleCodeList("CultuBarCode", 1).get(0);
                op.setAutoNo(a);
                op.setCultureNo(m.getCultrueNo());
                op.setCultureNa(m.getCultrueName());
                op.setCultureCombinNo(m.getCombinationNo());
                op.setCultureCombinNa(m.getCombinationNa());
                op.setIsPrintLabel(0);
                op.setPrintLabelCount(0);
                try {
                    op.setSeq(Integer.parseInt(m.getOrderNum()));
                } catch (Exception e) {
                    //TODO
                }
                if (ToolsUtils.isNotEmpty(labMaininfo)) {
                    op.setRptGroup(labMaininfo.getRptGroup());
                    op.setSampleda(labMaininfo.getSampleda());
                    op.setSampleNo(labMaininfo.getSampleNo());
                }
                BacTestCultureMediumList.add(op);
            }
        }

        saveWkflwNodesCultuMediums(BacTestWorkFlowList, BacTestWorkFlowNodeList, BacTestCultureMediumList);

        if (reqMain != null && BeanUtil.isNotEmpty(reqMain)) {
            reqMain.setPurposeNo("defaultmd");
            //this.saveOrUpdate(reqMain);
            labReqMainMapper.updateById(reqMain);
        }

        return true;
    }
}
